package com.hyts.mvc.base.interceptor;

import com.hyts.mvc.bean.web.MvcParamContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Context;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.mvc.base.execute
 * author:Libo/Alex
 * create-date:2019-07-05 13:06
 * email:liboware@gmail.com
 * description:此类主要用于:
 */
@Slf4j
public abstract class WebMvcInterceptor implements Interceptor<MvcParamContext> {

    /**
     * 前置处理
     * @param context
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(Context context) throws Exception {
        if(context != null && context instanceof MvcParamContext){
            try {
                return this.before((MvcParamContext)context).isResultValue();
            } catch (Exception e) {
                log.error("执行拦器失败!",e);
                return IntercepterResult.FAILURE.isResultValue();
            }
        }
        return IntercepterResult.SUCCESS.isResultValue();
    }

    /**
     * 后置处理
     * @param context
     * @param e
     * @return
     */
    @Override
    public boolean postprocess(Context context, Exception e) {
        log.error("执行拦截器出现异常",e);
        if(context != null && context instanceof MvcParamContext){
            try {
                return this.after((MvcParamContext)context).isResultValue();
            } catch (Exception e1) {
                log.error("执行拦器失败!",e1);
                return IntercepterResult.FAILURE.isResultValue();
            }
        }
        return IntercepterResult.SUCCESS.isResultValue();
    }
}
